using Assets.Scripts.Map.Data;
using Assets.Scripts.Map.Room;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;

public class SimpleRandomWalkDungeonGenerator : AbstractDungeonGenerator
{
    [SerializeField]
    protected DungeonManager dungeonManager;
    public List<RandomWalkSO> roomDataSOList;
    protected Dictionary<RoomType, RoomDataSO> roomDataSODict = new Dictionary<RoomType, RoomDataSO>();

    protected virtual void Init()
    {
        roomDataSODict.Clear();
        foreach (var item in roomDataSOList)
        {
            roomDataSODict.Add(item.roomType, item.randomWalkSO);
        }
    }

    protected override void RunProceduralGeneration()
    {
        HashSet<Vector2Int> floorPositions = RunRandomWalk(roomDataSOList[0].randomWalkSO,startPosition);
        tilemapVisualizer.Clear();
        tilemapVisualizer.PaintFloorTiles(floorPositions);
        WallGenerator.CreateWalls(tilemapVisualizer, floorPositions);
    }

    protected HashSet<Vector2Int> RunRandomWalk(RoomDataSO parameters,Vector2Int position)
    {
        Vector2Int currentPosition = position;
        HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();
        for (int i = 0; i < parameters.iterations; i++)
        {
            HashSet<Vector2Int> path = ProceduralGenerationAlgorithms.SimpleRandomWalk(currentPosition, roomDataSOList[0].randomWalkSO.walkLength);
            floorPositions.UnionWith(path);
            if(parameters.startRandomlyEachIteration)
            {
                currentPosition = floorPositions.ElementAt(Random.Range(0, floorPositions.Count));
            }
        }
        return floorPositions;
    }
}
[Serializable]
public class RandomWalkSO
{
    [SerializeField]
    public RoomType roomType;
    public RoomDataSO randomWalkSO;
}
